package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Station;
import com.xmy.cultivate.entity.StationNeeds;
import com.xmy.cultivate.service.IStationService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-01-31
 */
@RestController
@RequestMapping("/api/station")
public class StationController {

    @Autowired
    IStationService iStationService;

    @GetMapping()
    public Result findAll(
            @RequestParam(value = "pageNum",defaultValue = "") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "") Integer pageSize,
            @RequestParam(value = "name",defaultValue = "") String name){
        Map<String,Object> searchMap = new HashMap<>();
        Page<Station> page = new Page<>(pageNum,pageSize);

        QueryWrapper<Station> queryWrapper =  new QueryWrapper<>();
        //queryWrapper.apply("1=1");
        //queryWrapper.apply("FIND_IN_SET("+school_id+",school_ids)");
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);

        IPage<Station> iPage = iStationService.page(page,queryWrapper);

        return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }


    /**
     * 获取岗位列表
     * @return
     */
    @GetMapping("/stationList")
    public Result stationList(){

        QueryWrapper<Station> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("deleted",0);
        List<Station> stationList = iStationService.list(queryWrapper);
        return new Result(Code.OK,stationList,Code.GET_OK_MSG);
    }

    @GetMapping("/getStationListForSchool")
    public Result getStationListForSchool(@RequestParam("schoolIdOrDepartmentId") Long schoolIdOrDepartmentId){

        List<Station> stationList = iStationService.getStationListForSchool(schoolIdOrDepartmentId);
        return new Result(Code.OK,stationList,Code.GET_OK_MSG);
    }


    /**
     * 获取招聘名称
     * @return
     */
    @GetMapping("/recruitmentName")
    public Result recruitmentName(){

        List<Station> recruitmentName = iStationService.recruitmentName();
        return new Result(Code.OK,recruitmentName,Code.GET_OK_MSG);
    }

    @GetMapping("/getSchoolOrDepartList")
    public Result getSchoolOrDepartList(){

        List<Station> schoolOrDepartList = iStationService.getSchoolOrDepartList();
        return new Result(Code.OK,schoolOrDepartList,Code.GET_OK_MSG);
    }

    //@RequiresPermissions(value = "course:findbyid")
    @GetMapping(value = "/{id}")
    public Result findById(@PathVariable(value = "id") Long id){
        //System.out.print("findById");
        Station Station = iStationService.getById(id);
        return new Result(Code.OK,Station,"");
    }

    @PostMapping()
    public Result save(@RequestBody Station station){
        iStationService.save(station);
        //System.out.println(course);
        return new Result(Code.OK,"","");
    }

    @PutMapping("")
    public Result update(@RequestBody Station station){

        iStationService.updateById(station);
        return new Result(Code.OK);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
//        System.out.print("id:"+id);
        boolean rs =  iStationService.removeById(id);
        return new Result(rs?Code.OK:Code.ERROR);
    }
}
